package api

import (
	"gitee.com/eqyun/gosdk/sdk"
	"gitee.com/eqyun/gosdk/sdk/common"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"

	"gitee.com/eqyun/gosdk/sdk/pkg"
	"strings"
)

func GetRequestLogger(c *gin.Context) *log.Entry {
	l, ok := c.Get(pkg.LoggerKey)
	if ok {
		ok = false
		entity, ok := l.(*log.Entry)
		if ok {
			return entity
		}
	}
	//如果没有在上下文中放入logger
	requestId := pkg.GenerateMsgIDFromContext(c)

	contextLogger := sdk.Runtime.GetLogger().WithFields(log.Fields{
		"ip":                            common.GetClientIP(c),
		"time":                          pkg.GetCurrentTime(),
		"url":                           c.Request.URL.Path,
		"method":                        c.Request.Method,
		strings.ToLower(pkg.TrafficKey): requestId,
	})
	c.Set(pkg.LoggerKey, contextLogger)
	return contextLogger
}
